package com.darly.book.presenter;

import com.darly.book.model.flag.BookDistillate;
import com.darly.book.model.flag.BookSort;
import com.darly.book.model.flag.CommunityType;
import com.darly.book.model.remote.BookApi;
import com.darly.book.presenter.contract.DiscCommentContact;
import com.darly.book.ui.base.RxPresenter;
import com.darly.book.utils.Constant;
import com.darly.chinese.db.book.table.BookCommentBean;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.retrofit.RxjavaRetrofitRequestUtil;

import java.util.List;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class DiscCommentPresenter extends RxPresenter<DiscCommentContact.View> implements DiscCommentContact.Presenter {
    private static final String TAG = "DiscCommentPresenter";
    //是否采取直接从数据库加载
    private boolean isLocalLoad = true;

    @Override
    public void firstLoading(CommunityType block, BookSort sort, int start, int limited, BookDistillate distillate) {
        //获取数据库中的数据
//        Single<List<BookCommentBean>> localObserver = LocalRepository.getInstance()
//                .getBookComments(block.getNetName(), sort.getDbName(),
//                        start, limited, distillate.getDbName());
        Single<List<BookCommentBean>> remoteObserver = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookCommentList(block.getNetName(), "all", sort.getNetName(), "all", start + "", limited + "", distillate.getNetName())
                .map((listBean) -> listBean.getPosts());

        //这里有问题，但是作者却用的好好的，可能是2.0之后的问题
//        Disposable disposable = localObserver
//                .concatWith(remoteObserver)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (beans) -> {
//                            mView.finishRefresh(beans);
//                        }
//                        ,
//                        (e) -> {
//                            isLocalLoad = true;
//                            mView.complete();
//                            mView.showErrorTip();
//                            DLog.e(e);
//                        }
//                        ,
//                        () -> {
//                            isLocalLoad = false;
//                            mView.complete();
//                        }
//                );
//        addDisposable(disposable);
    }

    @Override
    public void refreshComment(CommunityType block, BookSort sort,
                               int start, int limited, BookDistillate distillate) {
        Disposable refreshDispo = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookCommentList(block.getNetName(), "all", sort.getNetName(), "all", start + "", limited + "", distillate.getNetName())
                .map((listBean) -> listBean.getPosts())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            isLocalLoad = false;
                            mView.finishRefresh(beans);
                            mView.complete();
                        }
                        ,
                        (e) -> {
                            mView.complete();
                            mView.showErrorTip();
                            DLog.e(e);
                        }
                );
        addDisposable(refreshDispo);
    }

    @Override
    public void loadingComment(CommunityType block, BookSort sort, int start, int limited, BookDistillate distillate) {
        if (isLocalLoad) {
//            Single<List<BookCommentBean>> Single = LocalRepository.getInstance()
//                    .getBookComments(block.getNetName(), sort.getDbName(),
//                            start, limited, distillate.getDbName());
//            loadComment(Single);
        } else {
            //单纯的加载数据
            Single<List<BookCommentBean>> Single = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                    .getBookCommentList(block.getNetName(), "all", sort.getNetName(), "all", start + "", limited + "", distillate.getNetName())
                    .map((listBean) -> listBean.getPosts());
            loadComment(Single);

        }
    }

    private void loadComment(Single<List<BookCommentBean>> Single) {
        Disposable loadDispo = Single.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            mView.finishLoading(beans);
                        }
                        ,
                        (e) -> {
                            mView.showError();
                            DLog.e(e);
                        }
                );
        addDisposable(loadDispo);
    }

    @Override
    public void saveComment(List<BookCommentBean> beans) {
//        LocalRepository.getInstance()
//                .saveBookComments(beans);
    }
}
